package com.faceye.core.manager.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;

import com.faceye.core.entity.BaseEntity;
import com.faceye.core.manager.BaseManager;
import com.faceye.core.repository.jpa.BaseRepository;

/**
 * Service基础类
 * 
 * @author @haipenge haipenge@gmail.com Create Date:2014年5月20日
 * @param <T>
 * @param <ID>
 */
public class BaseManagerImpl<T extends BaseEntity, ID extends Serializable, R extends BaseRepository<T, ID>>
		implements BaseManager<T, ID> {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected R repository = null;

	public BaseManagerImpl(R repository) {
		this.repository = repository;
	}

	@Override
	 public T save(T entity)  {
		return repository.save(entity);
	}

	@Override
	public List<T> save(Iterable<T> entities) {
		return repository.saveAll(entities);
	}

	@Override
	public T saveAndFlush(T entity) {
		return repository.saveAndFlush(entity);
	}

	@Override
	public T get(ID id) {
		return repository.getById(id);
	}

	@Override
	public void remove(ID id) {
		repository.deleteById(id);
	}

	@Override
	public void remove(T entity) {
		repository.delete(entity);
	}

	@Override
	public void removeAll() {
		repository.deleteAll();
	}

	@Override
	public void removeAllInBatch() {
		repository.deleteAllInBatch();
	}

	@Override
	public void removeInBatch(Iterable<T> entities) {
		repository.deleteInBatch(entities);
	}

	@Override
	public List<T> getAll() {
		return repository.findAll();
	}

	@Override
	public List<T> getAll(Iterable<ID> ids) {
		return repository.findAllById(ids);
	}

	/**
	 * page :1,2,3 分页查询
	 */
	@Override
	public Page<T> getPage(Map<String, Object> searchParams, int page, int size) {
		return repository.getPage(searchParams, page, size);
	}

}
